package com.easy.service.impl;

import com.easy.bean.User;
import com.easy.dao.IUserDao;
import com.easy.service.IUser;
import com.easy.util.JWTUtil;
import com.easy.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class UserImpl implements IUser {
    @Autowired
    IUserDao userD;


    //比较密码
    private boolean eqPassWord(String password,String password_data){
        return password.equals(password_data);
    }

    @Override
    public String doLogin(User user) {
        //1.获取数据库中的数据
        List<User> list=userD.getUserByUsername(user.getUsername());
        if(list.size()==1){
            User user_data=list.get(0);
            //2.比较密码
            boolean bool=eqPassWord(user.getUserpass(),user_data.getUserpass());
            if(bool){
                //3.生成token
                return JWTUtil.createToken(user.getUsername());
            }
        }

        return null;
    }

    @Override
    public boolean getUserByUsername(User user) {
            List<User> list=userD.getUserByUsername(user.getUsername());
            if(list.size()==1){
                User user_data=list.get(0);
                boolean bool=eqPassWord(user.getUserpass(), user_data.getUserpass());
                return bool;
            }else{
                return false;
            }
    }

    @Override
    public User getUser(int id) {
        return userD.getUser(id);
    }

    @Override
    public int editUser(User user) {
        int count=userD.editUser(user);
        return count;
    }


    @Override
    public List<User> searchUser(User user, Page page) {
        return userD.searchUser(user,page);
    }

    @Override
    public int getCount(User user) {
        return userD.getCount(user);
    }


    public int addUser(User user){
        user.setPosition("前台");
        int count=userD.addUser(user);
        return count;
    }

}
